package com.itheima.web;

import com.itheima.service.store.MemberService;
import com.itheima.service.store.OrderToTravellerService;
import com.itheima.service.store.OrdersService;
import com.itheima.service.store.impl.MemberServiceImpl;
import com.itheima.service.store.impl.OrderToTravellerServiceImpl;
import com.itheima.service.store.impl.OrdersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {

    protected OrdersService ordersService;
    protected MemberService memberService;
    protected OrderToTravellerService orderToTravellerService;

    /*
          init: 只执行一次
     */
    public void init(){

        ordersService = new OrdersServiceImpl();
        memberService = new MemberServiceImpl();
        orderToTravellerService = new OrderToTravellerServiceImpl();
    }
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            //设置编码
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");

            //获取操作名称
            String operation = req.getParameter("operation");
            Method method = this.getClass().getMethod(operation, HttpServletRequest.class, HttpServletResponse.class);
            //反射执行方法
            method.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
